from datetime import datetime
from app import db
import json


class UserProfile(db.Model):
    """用户详细资料表"""
    __tablename__ = 'user_profiles'
    
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False, comment='用户ID')
    
    # 教育信息
    education = db.Column(db.String(50), comment='学历（字典值）')
    school = db.Column(db.String(100), comment='毕业学校')
    major = db.Column(db.String(100), comment='专业')
    
    # 工作信息
    occupation = db.Column(db.String(50), comment='职业（字典值）')
    company = db.Column(db.String(200), comment='工作单位')
    position = db.Column(db.String(100), comment='职位')
    income_range = db.Column(db.String(50), comment='收入范围（字典值）')
    work_city = db.Column(db.String(50), comment='工作城市')
    
    # 家庭信息
    hometown = db.Column(db.String(100), comment='家乡')
    family_status = db.Column(db.String(50), comment='家庭状况')
    marital_status = db.Column(db.String(50), comment='婚姻状况（字典值）')
    
    # 生活信息
    housing = db.Column(db.String(50), comment='购房情况（字典值）')
    car = db.Column(db.String(50), comment='购车情况（字典值）')
    smoking_habit = db.Column(db.String(50), comment='吸烟习惯（字典值）')
    drinking_habit = db.Column(db.String(50), comment='饮酒习惯（字典值）')
    
    # 性格和爱好标签（JSON数组）
    personality_traits = db.Column(db.Text, comment='性格特征标签（JSON数组）')
    hobby_tags = db.Column(db.Text, comment='兴趣爱好标签（JSON数组）')
    
    # 其他信息
    nationality = db.Column(db.String(50), comment='民族')
    constellation = db.Column(db.String(20), comment='星座')
    blood_type = db.Column(db.String(5), comment='血型')
    hobbies = db.Column(db.Text, comment='兴趣爱好（文本描述）')
    self_intro = db.Column(db.Text, comment='自我介绍')
    
    # 时间戳
    created_at = db.Column(db.DateTime, default=datetime.utcnow, comment='创建时间')
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment='更新时间')
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'user_id': self.user_id,
            'education': self.education,
            'school': self.school,
            'major': self.major,
            'occupation': self.occupation,
            'company': self.company,
            'position': self.position,
            'income_range': self.income_range,
            'work_city': self.work_city,
            'hometown': self.hometown,
            'family_status': self.family_status,
            'marital_status': self.marital_status,
            'housing': self.housing,
            'car': self.car,
            'smoking_habit': self.smoking_habit,
            'drinking_habit': self.drinking_habit,
            'personality_traits': self.get_personality_traits_list(),
            'hobby_tags': self.get_hobby_tags_list(),
            'nationality': self.nationality,
            'constellation': self.constellation,
            'blood_type': self.blood_type,
            'hobbies': self.hobbies,
            'self_intro': self.self_intro,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S') if self.updated_at else None
        }
    
    def get_personality_traits_list(self):
        """获取性格特征标签列表"""
        if not self.personality_traits:
            return []
        try:
            return json.loads(self.personality_traits)
        except (json.JSONDecodeError, TypeError):
            return []
    
    def set_personality_traits_list(self, traits_list):
        """设置性格特征标签列表"""
        if isinstance(traits_list, (list, tuple)):
            self.personality_traits = json.dumps(traits_list)
        else:
            self.personality_traits = None
    
    def get_hobby_tags_list(self):
        """获取兴趣爱好标签列表"""
        if not self.hobby_tags:
            return []
        try:
            return json.loads(self.hobby_tags)
        except (json.JSONDecodeError, TypeError):
            return []
    
    def set_hobby_tags_list(self, hobby_list):
        """设置兴趣爱好标签列表"""
        if isinstance(hobby_list, (list, tuple)):
            self.hobby_tags = json.dumps(hobby_list)
        else:
            self.hobby_tags = None
    
    def __repr__(self):
        return f'<UserProfile {self.user_id}>' 